/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.logiclander.ricemine.services.identity;

import com.logiclander.ricemine.utils.LdapUtilities;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.dto.KimEntityAddressInfo;
import org.kuali.rice.kim.bo.entity.dto.KimEntityEmailInfo;
import org.kuali.rice.kim.bo.entity.dto.KimEntityEntityTypeInfo;
import org.kuali.rice.kim.bo.entity.dto.KimEntityPhoneInfo;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;

/**
 *
 * @author tcarroll
 */
public class LdapKimEntityEntityTypeInfoMapper implements ContextMapper {
  private final Log log = LogFactory.getLog(getClass());

  private LdapKimEntityEmailInfoMapper ldapKimEntityEmailInfoMapper = null;
  private LdapKimEntityPhoneInfoMapper ldapKimEntityPhoneInfoMapper = null;
  private LdapKimEntityAddressInfoMapper ldapKimEntityAddressInfoMapper = null;

//beg: methods required by ContextMapper interface
  public Object mapFromContext(Object ctx) {
    validateConfiguration();
    KimEntityEntityTypeInfo keeti = new KimEntityEntityTypeInfo();
    try {
      DirContextAdapter context = (DirContextAdapter) ctx;
      keeti.setEntityTypeCode("PERSON");//is there an ldap attribute for this?
//map emailAddresses
      List<KimEntityEmailInfo> keei = new ArrayList();
      keei.add((KimEntityEmailInfo) ldapKimEntityEmailInfoMapper.mapFromContext(ctx));
      keeti.setEmailAddresses(keei);
		  keeti.setDefaultEmailAddress((KimEntityEmailInfo)LdapUtilities.getKimDefaultItem(keei));
//map phoneNumbers
      List<KimEntityPhoneInfo> kepi = new ArrayList();
      kepi.add((KimEntityPhoneInfo) ldapKimEntityPhoneInfoMapper.mapFromContext(ctx));
      keeti.setPhoneNumbers(kepi);
		  keeti.setDefaultPhoneNumber((KimEntityPhoneInfo)LdapUtilities.getKimDefaultItem(kepi));
//map addresses
      List<KimEntityAddressInfo> keai = new ArrayList();
      keai.add((KimEntityAddressInfo)ldapKimEntityAddressInfoMapper.mapFromContext(ctx));
      keeti.setAddresses(keai);
		  keeti.setDefaultAddress((KimEntityAddressInfo)LdapUtilities.getKimDefaultItem(keai));

      keeti.setActive(true); //is there an ldap attribute for this?
    } catch (Throwable t) {
      log.error("Error in ContextMapper", t);
      String msg = "ContextMapper failed to create a LdapRecord from the specified Context: " + ctx;
      throw new RuntimeException(msg, t);
    }
    if (log.isDebugEnabled()) {
      log.debug("mapped LdapKimEntityEntityType:"
              + "(entityTypeCode=" + keeti.getEntityTypeCode() + ")"
              + "(isActive=" + keeti.isActive() + ")");
    }
    return keeti;
  }
//end: methods required by ContextMapper interface
//beg: helper methods
  public LdapKimEntityEmailInfoMapper getLdapKimEntityEmailInfoMapper() {
    return this.ldapKimEntityEmailInfoMapper;
  }

  public void setLdapKimEntityEmailInfoMapper(LdapKimEntityEmailInfoMapper ldapKimEntityEmailInfoMapper) {
    this.ldapKimEntityEmailInfoMapper = ldapKimEntityEmailInfoMapper;
  }

  public LdapKimEntityPhoneInfoMapper getLdapKimEntityPhoneInfoMapper() {
    return this.ldapKimEntityPhoneInfoMapper;
  }

  public void setLdapKimEntityPhoneInfoMapper(LdapKimEntityPhoneInfoMapper ldapKimEntityPhoneInfoMapper) {
    this.ldapKimEntityPhoneInfoMapper = ldapKimEntityPhoneInfoMapper;
  }

  public LdapKimEntityAddressInfoMapper getLdapKimEntityAddressInfoMapper() {
    return this.ldapKimEntityAddressInfoMapper;
  }

  public void setLdapKimEntityAddressInfoMapper(LdapKimEntityAddressInfoMapper ldapKimEntityAddressInfoMapper) {
    this.ldapKimEntityAddressInfoMapper = ldapKimEntityAddressInfoMapper;
  }

  private void validateConfiguration() {
    if (ldapKimEntityEmailInfoMapper == null) {
      String msg = "The property 'ldapKimEntityEmailInfoMapper' must be set.";
      throw new IllegalStateException(msg);
    }
    if (ldapKimEntityPhoneInfoMapper == null) {
      String msg = "The property 'ldapKimEntityPhoneInfoMapper' must be set.";
      throw new IllegalStateException(msg);
    }
    if (ldapKimEntityAddressInfoMapper == null) {
      String msg = "The property 'ldapKimEntityAddressInfoMapper' must be set.";
      throw new IllegalStateException(msg);
    }
  }
//end: helper methods

}
